package zelix.gui.clickguis.clickgui.shell;

import java.awt.Color;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.ScaledResolution;
import zelix.Core;
import zelix.gui.Notification.Utils.AnimationUtils;
import zelix.hack.Hack;
import zelix.hack.HackCategory;
import zelix.hack.hacks.ClickGui;
import zelix.utils.Wrapper;
import zelix.utils.hooks.visual.Colors;
import zelix.utils.hooks.visual.HGLUtils;
import zelix.utils.hooks.visual.RenderUtils;
import zelix.utils.hooks.visual.font.CFontRenderer;
import zelix.utils.hooks.visual.font.FontLoaders;
import zelix.value.BooleanValue;
import zelix.value.ModeValue;
import zelix.value.NumberValue;
import zelix.value.Value;

public class GuiClickShell extends GuiScreen {
	public static Minecraft mc = Minecraft.getMinecraft();
	public static ScaledResolution sr = new ScaledResolution(mc);
	public AnimationUtils animationUtils = new AnimationUtils();
	public static HackCategory currentModuleType;
	public static Hack currentModule;
	public static float startX = 100, startY = 40;
	public static int moduleStart = 0;
	public static int valueStart = 0;
	boolean previousmouse = true;
	boolean mouse;
	public float moveX = 0, moveY = 0;
	boolean bind = false;
	float hue;
	public static int alpha;
	public static int alphe = 121;
	int time = 0;

	int main_width = 450;
	int main_height = 296;

	int press = 0;

	private int modeIndex;
	private boolean animate = false;
	private float target = startY + 60;
	private float h = startY + 60;

	public GuiClickShell() {
		modeIndex = 0;

	}

	@Override
	public void drawScreen(int mouseX, int mouseY, float partialTicks) {

		CFontRenderer font = Core.fontLoaders.default16;
		sr = new ScaledResolution(mc);

		GL11.glPushMatrix();

		// Window
		HGLUtils.drawRoundedRect2(startX, startY, startX + main_width, startY + main_height, 4,
				Colors.getColor(29, 29, 31));
		RenderUtils.drawRect(startX + 101, startY + 51, startX + main_width, startY + main_height,
				Colors.getColor(38, 38, 41));
		// LOGO


		Core.fontLoaders.icon24.drawString("Zelix", startX + 10, startY + 20, Color.white.getRGB());


		GL11.glPopMatrix();
		// Line
		RenderUtils.drawRect(startX, startY + 50, startX + main_width, startY + 51, Colors.getColor(78, 78, 78));

		// test category width 60
		// RenderUtils.drawRect(startX + 110, startY + 10, startX + 160, startY + 40,
		// Color.white.getRGB());
		// RenderUtils.drawRect(startX + 170, startY + 10, startX + 220, startY + 40,
		// Color.white.getRGB());
		// RenderUtils.drawRect(startX + 230, startY + 10, startX + 280, startY + 40,
		// Color.white.getRGB());
		// RenderUtils.drawRect(startX + 290, startY + 10, startX + 340, startY + 40,
		// Color.white.getRGB());
		// RenderUtils.drawRect(startX + 350, startY + 10, startX + 400, startY + 40,
		// Color.white.getRGB());

		// Category
		// COMBAT
		if (this.isCategoryHovered(startX + 110, startY + 10, startX + 160, startY + 40, mouseX, mouseY)
				&& Mouse.isButtonDown((int) 0)) {
			currentModuleType = HackCategory.COMBAT;
			currentModule = Core.hackManager.getModulesInType(currentModuleType).get(0);
			moduleStart = 0;
			valueStart = 0;
			
			for (int x1 = 0; x1 < currentModule.getValues().size(); x1++) {
				Value value = currentModule.getValues().get(x1);
			}

		}
		if (currentModuleType == HackCategory.COMBAT) {
			Core.fontLoaders.default20.drawString("COMBAT", startX + 113, startY + 20, Colors.getColor(245, 245, 247));
		} else {
			Core.fontLoaders.default20.drawString("COMBAT", startX + 113, startY + 20, Colors.getColor(165, 165, 165));
		}
		// Movement
		if (this.isCategoryHovered(startX + 170, startY + 10, startX + 220, startY + 40, mouseX, mouseY)
				&& Mouse.isButtonDown((int) 0)) {
			currentModuleType = HackCategory.MOVEMENT;
			currentModule = Core.hackManager.getModulesInType(currentModuleType).get(0);
			moduleStart = 0;
			valueStart = 0;
			for (int x1 = 0; x1 < currentModule.getValues().size(); x1++) {
				Value value = currentModule.getValues().get(x1);
			}

		}

		if (currentModuleType == HackCategory.MOVEMENT) {
			Core.fontLoaders.default20.drawString("MOVEMENT", startX + 166, startY + 20, Colors.getColor(245, 245, 247));
		} else {
			Core.fontLoaders.default20.drawString("MOVEMENT", startX + 166, startY + 20, Colors.getColor(165, 165, 165));
		}
		// Player
		if (this.isCategoryHovered(startX + 230, startY + 10, startX + 280, startY + 40, mouseX, mouseY)
				&& Mouse.isButtonDown((int) 0)) {
			currentModuleType = HackCategory.PLAYER;
			currentModule = Core.hackManager.getModulesInType(currentModuleType).get(0);
			moduleStart = 0;
			valueStart = 0;
			if (currentModule.getValues() != null) {
				for (int x1 = 0; x1 < currentModule.getValues()
						.size(); x1++) {
					Value value = currentModule.getValues().get(x1);
				}

			}
		}
		if (currentModuleType == HackCategory.PLAYER) {
			Core.fontLoaders.default20.drawString("PLAYER", startX + 235, startY + 20, Colors.getColor(245, 245, 247));
		} else {
			Core.fontLoaders.default20.drawString("PLAYER", startX + 235, startY + 20, Colors.getColor(165, 165, 165));
		}

		// Client
		if (this.isCategoryHovered(startX + 290, startY + 10, startX + 340, startY + 40, mouseX, mouseY)
				&& Mouse.isButtonDown((int) 0)) {
			currentModuleType = HackCategory.ANOTHER;
			currentModule = Core.hackManager.getModulesInType(currentModuleType).get(0);
			moduleStart = 0;
			valueStart = 0;
			for (int x1 = 0; x1 < currentModule.getValues().size(); x1++) {
				Value value = currentModule.getValues().get(x1);
			}

		}

		if (currentModuleType == HackCategory.ANOTHER) {
			Core.fontLoaders.default20.drawString("CLIENT", startX + 292, startY + 20, Colors.getColor(245, 245, 247));
		} else {
			Core.fontLoaders.default20.drawString("CLIENT", startX + 292, startY + 20, Colors.getColor(165, 165, 165));
		}
		// Visual
		if (this.isCategoryHovered(startX + 350, startY + 10, startX + 400, startY + 40, mouseX, mouseY)
				&& Mouse.isButtonDown((int) 0)) {
			currentModuleType = HackCategory.VISUAL;
			currentModule = Core.hackManager.getModulesInType(currentModuleType).get(0);
			moduleStart = 0;
			valueStart = 0;
			if (currentModule.getValues() != null) {
				for (int x1 = 0; x1 < currentModule.getValues()
						.size(); x1++) {
					Value value = currentModule.getValues().get(x1);
				}
			}

		}

		if (currentModuleType == HackCategory.VISUAL) {
			Core.fontLoaders.default20.drawString("VISUAL", startX + 352, startY + 20, Colors.getColor(245, 245, 247));
		} else {
			Core.fontLoaders.default20.drawString("VISUAL", startX + 352, startY + 20, Colors.getColor(165, 165, 165));

		}

		if (currentModule == null){
			return;

		}
		int m = Mouse.getDWheel();
		// Line
		RenderUtils.drawRect(startX + 100, startY + 51, startX + 101, startY + main_height,
				Colors.getColor(78, 78, 78));

		if (this.isCategoryHovered(startX, startY + 51, startX + 100, startY + main_height, mouseX, mouseY)) {
			if (Core.hackManager.getModulesInType(currentModuleType) != null) {
				if (m < 0 && moduleStart < Core.hackManager.getModulesInType(currentModuleType).size() - 1) {
					moduleStart++;

				}
				if (m > 0 && moduleStart > 0) {
					moduleStart--;
				}
			}
		}

		if (this.isCategoryHovered(startX + 102, startY + 51, startX + main_width, startY + main_height, mouseX,
				mouseY)) {
			if (currentModule.getValues() != null) {
				if (m < 0 && valueStart < currentModule.getValues().size()
						- 1) {
					valueStart++;
				}
				if (m > 0 && valueStart > 0) {
					valueStart--;
				}
			}
		}

		float mY = startY + 6;
		for (int i = 0; i < Core.hackManager.getModulesInType(currentModuleType)
				.size(); i++) {
			Hack module = Core.hackManager.getModulesInType(currentModuleType).get(i);
			if (mY > startY + main_height - 70)
				break;
			if (i < moduleStart) {
				continue;
			}

			if (!module.isToggled()) {

				RenderUtils.drawRoundRect(startX + 400, startY + 260, startX + 440, startY + 280, 4,
						Colors.getColor(100, 100, 100));
				GL11.glEnable(GL11.GL_BLEND);
				FontLoaders.default18.drawCenteredString(bind ? "Press" : Keyboard.getKeyName(module.getKey()),
						startX + 420, startY + 270, Colors.getColor(255, 255, 255));

				GL11.glDisable(GL11.GL_BLEND);
				if (animate = false) {
					RenderUtils.drawRoundRect(startX + 12, mY + 55 - 4, startX + 14, mY + 55 + 10, 1,
							Colors.getColor(245, 245, 245));
				}
				GL11.glEnable(GL11.GL_BLEND);
				font.drawString(module.getName(), (int) startX + 20, (int) mY + 55, new Color(165, 165, 165).getRGB());
				GL11.glDisable(GL11.GL_BLEND);
			} else {

				RenderUtils.drawRoundRect(startX + 400, startY + 260, startX + 440, startY + 280, 4,
						Colors.getColor(100, 100, 100));
				GL11.glEnable(GL11.GL_BLEND);
				////System.out.println("Keyboard.getKeyName(module.getKey()) = " + Keyboard.getKeyName(module.getKey()));
				FontLoaders.default18.drawCenteredString(bind ? "Press" : Keyboard.getKeyName(module.getKey()),
						startX + 420, startY + 270, Colors.getColor(255, 255, 255));

				GL11.glDisable(GL11.GL_BLEND);
				if (animate = false) {
					RenderUtils.drawRoundRect(startX + 12, mY + 55 - 4, startX + 14, mY + 55 + 10, 1,
							Colors.getColor(245, 245, 245));
				}
				GL11.glEnable(GL11.GL_BLEND);
				font.drawString(module.getName(), (int) startX + 20, (int) mY + 55, new Color(245, 245, 247).getRGB());
				GL11.glDisable(GL11.GL_BLEND);
			}

			if (isSettingsButtonHovered(startX, mY + 45, startX + 100, mY + 65, mouseX, mouseY)) {
				if (!this.previousmouse && Mouse.isButtonDown(0)) {
					module.toggle();
					Core.fileManager.saveHacks();
					previousmouse = true;
				}
				if (!this.previousmouse && Mouse.isButtonDown(1)) {
					previousmouse = true;
				}

			}
			if (!Mouse.isButtonDown(0)) {
				this.previousmouse = false;
			}

			// Keybind

			if (Mouse.isButtonDown(0)){

			}
			if (isHovered(startX + 400, startY + 260, startX + 440, startY + 280, mouseX, mouseY) && Mouse.isButtonDown(0)) {
				this.bind = true;
				//keyTyped(Keyboard.getEventCharacter(), Keyboard.getEventKey());

			}

			if (isSettingsButtonHovered(startX, mY + 45, startX + 100, mY + 70, mouseX, mouseY)
					&& Mouse.isButtonDown(1)) {

				animate = true;
				target = mY + 54;
				// font.drawString("+", startX + 5, mY + 54, Color.green.getRGB());
				currentModule = module;
				valueStart = 0;

			}

			mY += 20;
		}
		mY = startY + 12;

		if (animate = true) {
			h = animationUtils.animate(target, h, 0.1f);
			// RenderUtils.drawRect(startX + 5, h, startX + 8, h + 5, Colors.getColor(245,
			// 245, 245));

			RenderUtils.drawRoundRect(startX + 12, h - 4, startX + 14, h + 10, 1, Colors.getColor(245, 245, 245));
			if (h == target) {
				animate = false;
			}

		}

		if (currentModule != null && currentModule.getDescription() != null) {

			Wrapper.INSTANCE.mc().fontRenderer.drawString(currentModule.getDescription(), (int) startX + 110,
					(int) mY + 275, new Color(245, 245, 245).getRGB());

		}

		if (currentModule != null){
			if (currentModule.getValues() != null) {
				for (int i = 0; i < currentModule.getValues().size(); i++) {
					if (mY > startY + 220)
						break;
					if (i < valueStart) {
						continue;
					}
					Value value = currentModule.getValues().get(i);

					if (value instanceof NumberValue) {
						float x = startX + 150;
						double render = (double) (
								200.0F * (((NumberValue) value).getValue() - ((NumberValue) value).getMin())
										/ (((NumberValue) value).getMax() - ((NumberValue) value).getMin()));

						RenderUtils.drawRoundRect(x + 2, mY + 65, x + 202, mY + 70, 3,
								isButtonHovered(x + 2, mY + 65, x + 202, mY + 70, mouseX, mouseY)
										? Colors.getColor(120, 120, 120)
										: Colors.getColor(81, 81, 84));
						RenderUtils.drawRoundRect(x + 2, mY + 65, x + render + 6.5D, mY + 70, 3,
								Colors.getColor(245, 245, 245));
						RenderUtils.drawRoundRect(x + render + 2D + 1, mY + 61, x + render + 2D + 5, mY + 65 + 9, 2,
								Colors.getColor(245, 245, 245));

						//

						GL11.glPushMatrix();
						GL11.glEnable(GL11.GL_BLEND);
						font.drawString(value.getName(), (int) startX + 130, (int) mY + 50,
								new Color(245, 245, 245).getRGB());
						font.drawString("" + value.getValue(),
								(int) startX + 140 - (int) font.getStringWidth("" + value.getValue()), (int) mY + 65,
								new Color(245, 245, 245).getRGB());

						GL11.glDisable(GL11.GL_BLEND);
						GL11.glPopMatrix();
						if (!Mouse.isButtonDown((int) 0)) {
							this.previousmouse = false;
						}

						if (this.isButtonHovered(x + 2, mY + 65, x + 202, mY + 70, mouseX, mouseY)
								&& Mouse.isButtonDown((int) 0)) {
							if (!this.previousmouse && Mouse.isButtonDown((int) 0)) {
								render = ((NumberValue) value).getMin();
								double max = ((NumberValue) value).getMax();
								double inc = 0.01;
								double valAbs = (double) mouseX - ((double) x + 1.0D);
								double perc = valAbs / 200.0D;
								perc = Math.min(Math.max(0.0D, perc), 1.0D);
								double valRel = (max - render) * perc;
								double val = render + valRel;
								val = (double) Math.round(val * (1.0D / inc)) / (1.0D / inc);
								value.setValue(val);
							}
							if (!Mouse.isButtonDown((int) 0)) {
								this.previousmouse = false;
							}
						}

						mY += 30;

						// SettingsSliderFile.saveState();

					}

					if (value instanceof BooleanValue) {

						float x = startX + 180;
						int xx = 30;
						int x2x = 45;

						GL11.glEnable(GL11.GL_BLEND);

						font.drawString(value.getName(), (int) startX + 150, (int) mY + 50,
								new Color(245, 245, 245).getRGB());

						GL11.glDisable(GL11.GL_BLEND);
						BooleanValue S = (BooleanValue)value;
						if (S.getValue()) {

							RenderUtils.drawRoundRect(startX + 130, mY + 46, startX + 130 + 14, mY + 50 + 10, 2,
									Colors.getColor(0, 0, 0));

							RenderUtils.drawRoundRect(startX + 130 + 1, mY + 46 + 1, startX + 130 + 14 - 1,
									mY + 50 + 10 - 1, 2, Colors.getColor(200, 200, 200));

						} else {

							RenderUtils.drawRoundRect(startX + 130, mY + 46, startX + 130 + 14, mY + 50 + 10, 2,
									Colors.getColor(0, 0, 0));

						}
						if (this.isCheckBoxHovered(startX + 130, mY + 46, startX + 130 + 14, mY + 50 + 10, mouseX,
								mouseY)) {
							RenderUtils.drawRect(startX + 130 + 1, mY + 46 + 1, startX + 130 + 14 - 1, mY + 50 + 10 - 1,
									Colors.getColor(60, 60, 60));

							if (!this.previousmouse && Mouse.isButtonDown((int) 0)) {
								this.previousmouse = true;
								this.mouse = true;
							}

							if (this.mouse) {

								S.setValue(!S.getValue());
								this.mouse = false;
							}
						}
						if (!Mouse.isButtonDown(0)) {
							this.previousmouse = false;
						}
						mY += 20;
						// SettingsButtonFile.saveState();

					}

					if (value instanceof ModeValue) {

						float x = startX + 130;
						GL11.glEnable(GL11.GL_BLEND);
						font.drawStringWithShadow(value.getName(), startX + 130, mY + 52,
								new Color(245, 245, 245).getRGB());
						GL11.glDisable(GL11.GL_BLEND);
						/*
						 * RenderUtils.drawRect(x + 5, mY + 45, x + 75, mY + 65, isStringHovered(x, mY +
						 * 45, x + 75, mY + 65, mouseX, mouseY) ? new Color(80, 80, 80).getRGB() : new
						 * Color(56, 56, 56).getRGB()); RenderUtils.drawRect(x + 2, mY + 48, x + 78, mY
						 * + 62, isStringHovered(x, mY + 45, x + 75, mY + 65, mouseX, mouseY) ? new
						 * Color(80, 80, 80).getRGB() : new Color(56, 56, 56).getRGB());
						 * RenderUtils.drawFilledCircle(x + 5, mY + 48, 3, isStringHovered(x, mY + 45, x
						 * + 75, mY + 65, mouseX, mouseY) ? new Color(80, 80, 80).getRGB() : new
						 * Color(56, 56, 56).getRGB(), 5); RenderUtils.drawFilledCircle(x + 5, mY + 62,
						 * 3, isStringHovered(x, mY + 45, x + 75, mY + 65, mouseX, mouseY) ? new
						 * Color(80, 80, 80).getRGB() : new Color(56, 56, 56).getRGB(), 5);
						 * RenderUtils.drawFilledCircle(x + 75, mY + 48, 3, isStringHovered(x, mY + 45,
						 * x + 75, mY + 65, mouseX, mouseY) ? new Color(80, 80, 80).getRGB() : new
						 * Color(56, 56, 56).getRGB(), 5); RenderUtils.drawFilledCircle(x + 75, mY + 62,
						 * 3, isStringHovered(x, mY + 45, x + 75, mY + 65, mouseX, mouseY) ? new
						 * Color(80, 80, 80).getRGB() : new Color(56, 56, 56).getRGB(), 5);
						 */

						// RenderUtils.drawBorderedRect(x, y, 3,
						// Colors.getColor(37,37,37),Colors.getColor(91,91,91));

						RenderUtils.drawRoundRect(x, mY + 62, x + 100, mY + 76, 2, Colors.getColor(80, 80, 80));
						ModeValue M = (ModeValue)value;

						if (M.getValue() != null) {
							GL11.glEnable(GL11.GL_BLEND);
							font.drawStringWithShadow(M.getValue().getName(),
									x + 18 - font.getStringWidth(M.getValue().getName()) / 2,
									mY + 66,
									new Color(220, 220, 220).getRGB()
							);
							GL11.glDisable(GL11.GL_BLEND);
						}
						if (this.isStringHovered(x, mY + 62, x + 100, mY + 76, mouseX, mouseY)) {
							if (!this.previousmouse && Mouse.isButtonDown((int) 0)) {
								this.previousmouse = true;
								this.mouse = true;
							}

							if (this.mouse) {
								int maxIndex = M.getModes().length;
								modeIndex++;

								if (modeIndex + 1 > maxIndex)
									modeIndex = 0;


								M.setValue(M.getModes()[modeIndex]);
								//////System.out.println("M.getValue() = " + M.getValue().getName());

								this.mouse = false;
							}

							if (!Mouse.isButtonDown(0)) {
								this.previousmouse = false;
							}

						}
						mY += 30;
					}

					// SettingsComboBoxFile.saveState();

				}

				// float x = startX + 300;
				// float yyy = startY + 200;

				if ((isHovered(startX, startY, startX + 100, startY + 50, mouseX, mouseY)
						|| isHovered(startX, startY + 315, startX + 450, startY + 350, mouseX, mouseY)
						|| isHovered(startX + 430, startY, startX + 450, startY + 350, mouseX, mouseY))
						&& Mouse.isButtonDown(0)) {
					if (moveX == 0 && moveY == 0) {
						moveX = mouseX - startX;
						moveY = mouseY - startY;
					} else {
						startX = mouseX - moveX;
						startY = mouseY - moveY;
					}
					this.previousmouse = true;
				} else if (moveX != 0 || moveY != 0) {
					moveX = 0;
					moveY = 0;
				}
				int j = 59;
				int l = 40;
				float k = startY + 10;
				float xx = startX + 5;
				float typey;
			}
		}


		super.drawScreen(mouseX, mouseY, partialTicks);
	}

	@Override
	public void onGuiClosed() {
		super.onGuiClosed();
		ClickGui.shell_x = startX;
		ClickGui.shell_y = startY;
		// ClickGuiModule.memoriseWheel = wheel;
		// ClickGuiModule.memoriseML = inSetting;
		ClickGui.shell_category = currentModuleType;
		ClickGui.shell_module = currentModule;

		Core.fileManager.saveClickGui();
		Core.fileManager.saveHacks();

		try {
			Mouse.setNativeCursor(null);
		} catch (Throwable ignore) {
		}
	}

	public boolean isStringHovered(float f, float y, float g, float y2, int mouseX, int mouseY) {
		if (mouseX >= f && mouseX <= g && mouseY >= y && mouseY <= y2) {
			return true;
		}

		return false;
	}

	public boolean isSettingsButtonHovered(float x, float y, float x2, float y2, int mouseX, int mouseY) {
		if (mouseX >= x && mouseX <= x2 && mouseY >= y && mouseY <= y2) {
			return true;
		}

		return false;
	}

	public boolean isButtonHovered(float f, float y, float g, float y2, int mouseX, int mouseY) {
		if (mouseX >= f && mouseX <= g && mouseY >= y && mouseY <= y2) {
			return true;
		}

		return false;
	}

	public boolean isCheckBoxHovered(float f, float y, float g, float y2, int mouseX, int mouseY) {
		if (mouseX >= f && mouseX <= g && mouseY >= y && mouseY <= y2) {
			return true;
		}

		return false;
	}

	public boolean isCategoryHovered(float x, float y, float x2, float y2, int mouseX, int mouseY) {
		if (mouseX >= x && mouseX <= x2 && mouseY >= y && mouseY <= y2) {
			return true;
		}

		return false;
	}

	public boolean isHovered(float x, float y, float x2, float y2, int mouseX, int mouseY) {
		if (mouseX >= x && mouseX <= x2 && mouseY >= y && mouseY <= y2) {
			return true;
		}

		return false;
	}

	@Override
	public void keyTyped(char typedChar, int keyCode) {
		if (this.bind) {
			currentModule.setKey(keyCode);
			if (keyCode == 1) {
				currentModule.setKey(0);
			}
			this.bind = false;
		} else if (keyCode == 1) {
			this.mc.displayGuiScreen((GuiScreen) null);
			if (this.mc.currentScreen == null) {
				this.mc.setIngameFocus();
			}
			return;
		}
	}

	public static void setMod(Hack mod) {
		currentModule = mod;
	}

	public static void setX(float shell_x) {
		startX = shell_x;
	}

	public static void setY(float shell_y) {
		startY = shell_y;
	}

	public static void setCategory(HackCategory state) {
		currentModuleType = state;
	}

}
